package com.google.code.opengw.server.domain;

import java.net.Socket;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

import com.google.code.opengw.support.OpenGWException;

public class ConnectionQueue {
	private int queueSize;
	private Queue<Socket> socketQueue;
	
	public ConnectionQueue(int poolSize){
		socketQueue = new LinkedList<Socket>();
		this.queueSize = poolSize;
	}
	
    public synchronized void addConnection(Socket socket) throws OpenGWException {
    	
    	if(socketQueue.size() < queueSize)
    		socketQueue.add(socket);
    	else
    		throw new OpenGWException(OpenGWException.QUEUE_SIZE_EXCEED);
    }
    
    public synchronized Socket getConnection(){
    	if(!socketQueue.isEmpty())
    		return socketQueue.poll();
    	
    	throw new NoSuchElementException();
    }
    
    public boolean isEmpty(){
    	return socketQueue.isEmpty();
    }
    
    public int size(){
    	return socketQueue.size();
    }
}
